package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.business.dao.sys.SysPositionMapper;
import com.cloud.business.entity.sys.SysPosition;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.syspositionvm.SysPositionQueryModel;
import com.cloud.business.service.sys.ISysPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 系统职位 服务实现类
 * </p>
 *
 * @author dh
 * @since 2020-03-21
 */
@Service
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPosition> implements ISysPositionService {

    @Resource
    private SysPositionMapper sysPositionMapper;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysPositionQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysPositionQueryModel sysPositionQueryModel, IPage<SysPosition> page, ResultMsg msg) throws Exception {
        QueryWrapper<SysPosition> sysPositionQueryWrapper = new QueryWrapper<>();
        sysPositionQueryWrapper.lambda().like(!StringUtils.isEmpty(sysPositionQueryModel.getQueryStr()), SysPosition::getName, sysPositionQueryModel.getQueryStr())
                .eq(sysPositionQueryModel.getStructId() != null && sysPositionQueryModel.getStructId() != 0
                        , SysPosition::getStructId, sysPositionQueryModel.getStructId())
                ;
        sysPositionMapper.selectPage(page, sysPositionQueryWrapper);
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param sysPosition
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysPosition sysPosition, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysPosition.setCreateTime(now);
        sysPosition.setModificationTime(now);
        int count = sysPositionMapper.insert(sysPosition);
        msg.setMsg("创建成功");
        return sysPosition.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param sysPosition
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysPosition sysPosition, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysPosition.setModificationTime(now);
        int count = sysPositionMapper.updateById(sysPosition);
        msg.setMsg("更新成功");
        return sysPosition.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysPosition getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysPosition sysPosition = sysPositionMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysPosition;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysPositionMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion
}
